package cn.yunyichina.reservation.component.qiniu;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by Lullaby on 2016/8/27.
 */
@Component
public class QiniuService {

    private static final Logger logger = LoggerFactory.getLogger(QiniuService.class);

    public Response upload(byte[] bytes, String bucket) {
        try {
            //调用put方法上传
            Response res = Qiniu.INSTANCE.manager().put(bytes, null, Qiniu.INSTANCE.auth().uploadToken(bucket));
            //打印返回的信息
            logger.info(res.bodyString());
            return res;
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            logger.error("[QiniuService exception info]", r.toString());
            //响应的文本信息
            try {
                logger.error("[QiniuService response info]", r.bodyString());
            } catch (QiniuException qe) {
                logger.error("[QiniuService response exception info]", qe);
            }
            return null;
        }
    }

}
